#!/usr/bin/env python
#coding=utf-8
"""
Author:         Xia Kai <xiaket@gmail.com>
Filename:       models.py
Type:           Class definition
Last modified:  2010-05-24 22:27

Description:
This file contains a class that would turn a dictionary containing user
information as was returned by json request into a T163User class. So we may
more convienently retrieve user information.
"""
from utils import parse_timestring


class T163UserBase(object):
    """
    Basic user information that can be public accessible.
    User dictionary = {
        profile_image_url_large         用户资料的大图片 80*80
        id                              用户id
        profile_image_url_small         用户资料的小图片 24*24
        verified                        已验证(为名人预留的?)
        reply_type                      回复类型
        profile_sidebar_fill_color      
        profile_text_color              
        followers_count                 fo这个用户的人数
        location                        这个用户所处的地理位置
        profile_background_color        
        utc_offset                      
        statuses_count                  用户微博数
        description                     用户个人描述
        friends_count                   这个用户fo的人数
        profile_link_color              
        profile_image_url               
        profile_background_image_url    用户资料的小图片 48*48
        screen_name                     屏幕显示名, 更常用
        profile_background_tile         
        favourites_count                
        name                            用户名
        url                             链接
        gender                          性别
        created_at                      注册时间
        time_zone                       时区
        profile_sidebar_border_color    
    """
    def __init__(self, userdict):
        """
        Long and tedious initilization process.
        """
        # account information
        self.id = userdict['id']
        self.screen_name = userdict['screen_name']
        self.name = userdict['name']

        # user profile
        self.url = userdict['url']
        self.description = userdict['description']
        self.location = userdict['location']

        # following information
        self.followers_count = userdict['followers_count']
        self.statuses_count = userdict['statuses_count']
        self.friends_count = userdict['friends_count']
        self.favourites_count = userdict['favourites_count']

        # Gender is made more human readable.
        if userdict['gender'] == 0:
            self.gender = 'M'
        elif userdict['gender'] == 1:
            self.gender = 'F'
        else:
            self.gender = 'U'

        # Created_at is translated into a python datetime object.
        self.created_at = parse_timestring(userdict['created_at'])

        # these are not implemented yet, so we comment'em'out.
        """
        # account information
        self.verified = userdict['verified']

        # user profile
        self.time_zone = userdict['time_zone']
        self.utc_offset = userdict['utc_offset']

        # avatar image urls.
        self.image_large = userdict['profile_image_url_large']
        self.image_medium = userdict['profile_image_url']
        self.image_small = userdict['profile_image_url_small']

        # user homepage appearance.
        self.profile_sidebar_fill_color = \
            userdict['profile_sidebar_fill_color']
        self.profile_text_color = userdict['profile_text_color']
        self.profile_background_color = userdict['profile_background_color']
        self.profile_link_color = userdict['profile_link_color']
        self.profile_background_image_url = \
            userdict['profile_background_image_url']
        self.profile_background_tile = userdict['profile_background_tile']
        self.profile_sidebar_border_color = \
            userdict['profile_sidebar_border_color']

        # unknown...
        self.reply_type = userdict['reply_type']
        """


class User(T163UserBase):
    """
    Additional user information is stored in the following dictionary:
    User dictionary = {
        telephone                       用户手机号
        email                           用户邮箱
    }
    """
    def __init__(self, userdict):
        T163UserBase.__init__(self, userdict)

        # additional user profile
        self.telephone = userdict['telephone']
        self.email = userdict['email']


class Follower(T163UserBase):
    """
    This class is used to store information for followers, apart from those
    properties defined in T163UserBase, this class has the following
    information:

    Follower dictionary = {
        followed_by     这个用户是否在follow你
        status          这个用户最新的一条推的详细信息
        following       你是否在follow这个用户
    """
    def __init__(self, userdict):
        T163UserBase.__init__(self, userdict)
        self.followed_by = userdict['followed_by']
        self.status = userdict['status']
        self.following = userdict['following']


class T163StatusBase(object):
    """
    This class is used to store basic status information. The status 
    information is provided in a dictionary:

    Status dictionary = {
        user_id                 用户id
        truncated               未知
        text                    推内容
        created_at              发推时间
        retweet_status_id       最初的被retweeted的消息的id.
        source                  网易微博
        in_reply_to_status_id   None
        in_reply_to_screen_name None
        in_reply_to_user_id     None
        type                    未知
        id                      本消息id
    }
    """
    def __init__(self, status_dict):
        self.user_id = status_dict['user_id']
        self.text = status_dict['text']
        self.created_at = parse_timestring(status_dict['created_at'])
        self.retweet_status_id = status_dict['retweet_status_id']
        self.source = status_dict['source']
        self.id = status_dict['id']
        self.in_reply_to_status_id = status_dict['in_reply_to_status_id']
        self.in_reply_to_screen_name = status_dict['in_reply_to_screen_name']
        self.in_reply_to_user_id = status_dict['in_reply_to_user_id']

        # these are not implemented yet, so we comment'em'out.
        """
        self.truncated = status_dict['truncated']
        self.type = status_dict['type']
        """


class Status(T163StatusBase):
    """
    This class is for the show() api, which is used to show the detailed
    information for a tweet.

    Additional information:
        favorited                   False
        in_reply_to_status_text     None
        favorited_at                None
        in_reply_to_user_name       None
        user                         
    """
    def __init__(self, status_dict):
        T163StatusBase.__init__(self, status_dict)
        self.user = T163UserBase(status_dict['user'])

        # these are not implemented yet, so we comment'em'out.
        """
        self.favorited = status_dict['favorited']
        self.in_reply_to_status_text = status_dict['in_reply_to_status_text']
        self.favorited_at = status_dict['favorited_at']
        self.in_reply_to_user_name = status_dict['in_reply_to_user_name']
        """


class StatusWithIpaddr(T163StatusBase):
    """
    This class is for the show() api, which is used to show the detailed
    information for a tweet.

    Additional information:
        auditStatus             未知
        ipaddr                  117.84.92.50
    """
    def __init__(self, status_dict):
        T163StatusBase.__init__(self, status_dict)
        self.ipaddr = status_dict['ipaddr']


class DirectMessage(object):
    """
        sender_screen_name corleone
        followed_by True
        sender T163UserBaseObject
        text 测试内容啊啊
        created_at Tue Apr 27 20:40:58 +0800 2010
        sender_id -5127315299555819730
        recipient_id 6493809605159984224
        recipient_screen_name xiaket
        recipient T163UserBaseObject
        id 7950999978748591002
    """
    def __init__(self, messagedict):
        # message
        self.id = messagedict['id']
        self.text = messagedict['text']
        self.created_at = parse_timestring(messagedict['created_at'])

        # sender
        self.sender = T163UserBase(messagedict['sender'])
        self.sender_id = messagedict['sender_id']
        self.sender_screen_name = messagedict['sender_screen_name']
        self.followed = messagedict['followed_by']

        # recipient
        self.recipient = T163UserBase(messagedict['recipient'])
        self.recipient_id = messagedict['recipient_id']
        self.recipient_screen_name = messagedict['recipient_screen_name']


class SearchHit(T163StatusBase):
    """
    This class is for the show() api, which is used to show the detailed
    information for a tweet.

    Additional information:
        favorited                   False
        favorited_at                None
        in_reply_to_user_name       None
        user                         
    """
    def __init__(self, status_dict):
        T163StatusBase.__init__(self, status_dict)
        self.user = T163UserBase(status_dict['user'])

        # these are not implemented yet, so we comment'em'out.
        """
        self.favorited = status_dict['favorited']
        self.favorited_at = status_dict['favorited_at']
        self.in_reply_to_user_name = status_dict['in_reply_to_user_name']
        """


class SearchResult(object):
    """
        totalHits 14973
        next_page
        completed_in 0
        availHits 600
        refresh_url
        since_id 0
        results_per_page 30
        result
        query
        max_id 0
        page 1
    """
    def __init__(self, result_dict):
        self.totalHits = result_dict['totalHits']
        self.next_page = result_dict['next_page']
        self.completed_in = result_dict['completed_in']
        self.availHits = result_dict['availHits']
        self.refresh_url = result_dict['refresh_url']
        self.since_id = result_dict['since_id']
        self.results_per_page = result_dict['results_per_page']
        self.result = []
        for item in result_dict['result']:
            self.result.append(SearchHit(item))
        self.query = result_dict['query']
        self.max_id = result_dict['max_id']
        self.page = result_dict['page']


class UserSearchResult(object):
    """
    totalHits   number
    availHits   number
    result      list
    """
    def __init__(self, result_dict):
        self.totalHits = result_dict['totalHits']
        self.availHits = result_dict['availHits']
        self.result = []
        for item in result_dict['result']:
            self.result.append(UserSearchHit(item))


class UserSearchHit(T163UserBase):
    """
    Additional information stored in the search result:
        telephone       always null
        email           always null
        status          StatusWithIpaddr
        following       False
    }
    """
    def __init__(self, userdict):
        T163UserBase.__init__(self, userdict)

        # additional user profile
        self.status = StatusWithIpaddr(userdict['status'])
        self.following = userdict['following']
